package com.it.od.new202211;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;

/**
 * @author: liminghui
 * @date: 2021/7/25 11:37
 * @version: 1.0
 * @description: 组合，大集合套小集合 拆分 每个小集合取一个元素
 * 输入
 * [[a,b],[男，女]，[1,2,3]]
 * 输出
 * [a, 男, 1]
 * [a, 男, 2]
 * [a, 男, 3]
 * [a, 女, 1]
 * [a, 女, 2]
 * [a, 女, 3]
 * [b, 男, 1]
 * [b, 男, 2]
 * [b, 男, 3]
 * [b, 女, 1]
 * [b, 女, 2]
 * [b, 女, 3]
 */
public class SplitOneByOne {
    public static void main(String[] args) throws Exception {
        List<List<String>> lists = new ArrayList<>();
        List<String> list1 = new ArrayList<>();
        list1.add("a");
        list1.add("b");
        lists.add(list1);
        List<String> list2 = new ArrayList<>();
        list2.add("男");
        list2.add("女");
        lists.add(list2);
        List<String> list3 = new ArrayList<>();
        list3.add("1");
        list3.add("2");
        list3.add("3");
        lists.add(list3);

        List<Object> originalList = lists.stream().map(x -> (Object) x).collect(Collectors.toList());


        // 存放结果的集合
        List<List<String>> resultList = new ArrayList<>();

        Queue<TempPojo> queue = new LinkedList<>();
        TempPojo tempPojo = new TempPojo();
        tempPojo.setStep(0);
        tempPojo.setPojoList(originalList);
        queue.add(tempPojo);

        while (queue.size() > 0) {
            TempPojo tempPojo1 = queue.poll();
            int step = tempPojo1.getStep();
            List<Object> pojoList = tempPojo1.getPojoList();

            // 1步长与初始集合长度相同，已经拆解到最后一个集合.拆解后将结果放入resultList
            if (step == originalList.size() - 1) {
                // 拷贝前N-1项
                List<String> subList = new ArrayList<>();
                for (int i = 0; i < step; i++) {
                    subList.add((String) pojoList.get(i));
                }

                List<String> list = (List<String>) pojoList.get(step);
                for (String str : list) {
                    List<String> subResultList = new ArrayList<>();
                    subResultList.addAll(subList);
                    subResultList.add(str);
                    resultList.add(subResultList);
                }

            } else { // 2如果步长小于初始集合长度，拆解后，构建pojo并加入队列. [前step-1项+第step项+step后集合]

                List<String> subList1 = new ArrayList<>();
                for (int i = 0; i < step; i++) {
                    subList1.add((String) pojoList.get(i));
                }
                List<List<String>> subList2 = new ArrayList<>();
                for (int i = step + 1; i < pojoList.size(); i++) {
                    subList2.add((List<String>) pojoList.get(i));
                }

                List<String> tempList1 = (List<String>) pojoList.get(step);
                for (String str : tempList1) {
                    List<Object> subTempList = new ArrayList<>();
                    subTempList.addAll(subList1);
                    subTempList.add(str);
                    subTempList.addAll(subList2);

                    TempPojo tempPojo2 = new TempPojo();
                    tempPojo2.setStep(step + 1);
                    tempPojo2.setPojoList(subTempList);

                    queue.add(tempPojo2);
                }
            }
        }

        for (List<String> strings : resultList) {
            System.out.println(strings);
        }

    }

    public static class TempPojo {
        private int step;//步长，代表要拆解第几个小集合
        private List<Object> pojoList;//代表要拆解的集合

        public int getStep() {
            return step;
        }

        public void setStep(int step) {
            this.step = step;
        }

        public List<Object> getPojoList() {
            return pojoList;
        }

        public void setPojoList(List<Object> pojoList) {
            this.pojoList = pojoList;
        }
    }
}
